using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using umbraco;
using umbraco.BasePages;
using umbraco.BusinessLogic;
using umbraco.interfaces;
using umbraco.controls;
using Commerce.Common;

namespace CommerceForUmbraco.adminSection.commerce
{
    public partial class editMailer : umbraco.BasePages.UmbracoEnsuredPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string mailerId = Request.QueryString.Get("mailer");
                MessagingMailer thisMailer = new MessagingMailer(int.Parse(mailerId));
                MailerName.Text = thisMailer.MailerName;
                toList.Text = thisMailer.ToList;
                ccList.Text = thisMailer.CcList;
                fromName.Text = thisMailer.FromName;
                fromEmail.Text = thisMailer.FromEmail;
                subject.Text = thisMailer.Subject;
                messageBody.Text = thisMailer.MessageBody;
            }

                this.Panel1.Text = "Edit Category";
                this.pp_name.Text = "Category Name";
                this.pp_to_list.Text = "To List";
                this.pp_cc_list.Text = "CC List";
                this.pp_from_name.Text = "From Name";
                this.pp_from_email.Text = "From Email";
                this.pp_subject.Text = "Subject";
                this.pp_message_body.Text = "Message Body";
        }

        private void InitializeComponent()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            ImageButton button = this.Panel1.Menu.NewImageButton();
            button.Click += new ImageClickEventHandler(this.save_click);
            button.AlternateText = "Save";
            button.ImageUrl = GlobalSettings.Path + "/images/editor/save.gif";
            base.Load += new EventHandler(this.Page_Load);
            this.InitializeComponent();
            base.OnInit(e);
        }

        private void save_click(object sender, ImageClickEventArgs e)
        {
            if (this.SaveMailer(base.Request.QueryString["mailer"]))
            {
                base.speechBubble(0, ui.Text("speechBubbles", "commerceItemSaved"), "");
            }
            else
            {
                base.speechBubble(0, ui.Text("speechBubbles", "commerceItemErrorHeader"), ui.Text("speechBubbles", "commerceItemErrorText"));
            }
        }

        private bool SaveMailer(string mailerId)
        {
            try
            {
                MessagingMailer thisMailer = new MessagingMailer(int.Parse(mailerId));
                thisMailer.MailerName = this.MailerName.Text;
                thisMailer.ToList = this.toList.Text;
                thisMailer.CcList = this.ccList.Text;
                thisMailer.FromName = this.fromName.Text;
                thisMailer.FromEmail = this.fromEmail.Text;
                thisMailer.Subject = this.subject.Text;
                thisMailer.MessageBody = this.messageBody.Text;

                thisMailer.Save();

                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
